<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="业务线" prop="bizCode">
        <el-select
            v-model="queryParams.bizCode"
            style="width: 200px"
            placeholder="请先选择业务线"
            clearable
            filterable
            size="small"
            @keyup.enter.native="handleQuery"
          >
            <el-option
              :label="dict.dictLabel"
              :value="dict.dictValue"
              v-for="dict in bizDisplayOptions"
              :key="dict.dictLabel"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="菜单" prop="menuName">
        <el-input
          v-model="queryParams.menuName"
          placeholder="请输入菜单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="访问用户" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择访问用户"
          clearable
          filterable
          size="small"
          style="width: 215px"
          remote
          @focus="searchUser()"
          :remote-method="searchUser"
        >
          <el-option
            v-for="item in createByOptions"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="访问时间" prop="visitTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.visitTime2"
          style="width: 215px"
          @change="handleVisitTimeChange"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @handleQuery="handleQuery" @resetQuery="resetQuery" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="menuVisitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="业务线编码" align="center" prop="bizCode" />
      <el-table-column label="菜单名称" align="center" prop="menuName" />
      <el-table-column label="路由地址" align="center" prop="path" />
      <el-table-column label="组件路径" align="center" prop="component" />
      <el-table-column label="主机地址" align="center" prop="ip" />
      <el-table-column label="访问用户" align="center" prop="userName" />
      <el-table-column label="访问时间" align="center" prop="visitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.visitTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listMenuVisit, getMenuVisit, delMenuVisit, addMenuVisit, updateMenuVisit } from "@/api/system/menuVisit";
import {
  listUser
} from "@/api/common/label";

export default {
  name: "MenuVisit",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 业务线字典
      bizDisplayOptions: [],
      //创建人
      createByOptions: [],
      // 总条数
      total: 0,
      // 菜单访问日志记录表格数据
      menuVisitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bizCode: null,
        menuId: null,
        menuName: null,
        parentId: null,
        path: null,
        component: null,
        ip: null,
        userId: null,
        userName: null,
        visitTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bizCode: [
          { required: true, message: "业务线编码不能为空", trigger: "blur" }
        ],
        menuId: [
          { required: true, message: "菜单ID不能为空", trigger: "blur" }
        ],
        menuName: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" }
        ],
        parentId: [
          { required: true, message: "父菜单ID不能为空", trigger: "blur" }
        ],
        path: [
          { required: true, message: "路由地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  async created() {
    let resArr = await Promise.all([
        // 获取业务线字典
        this.getDicts("BAC_BIZ_DISPLAY"),
      ]);
      this.bizDisplayOptions = resArr[0].data;
    this.getList();
  },
  methods: {
    /** 查询菜单访问日志记录列表 */
    getList() {
      this.loading = true;
      listMenuVisit(this.queryParams).then(response => {
        this.menuVisitList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bizCode: null,
        menuId: null,
        menuName: null,
        parentId: null,
        path: null,
        component: null,
        ip: null,
        userId: null,
        userName: null,
        visitTime: null
      };
      this.resetForm("form");
    },
    //查询所有用户信息
    searchUser(name) {
      listUser({ realName: name }).then((response) => {
        this.createByOptions = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜单访问日志记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMenuVisit(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜单访问日志记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMenuVisit(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMenuVisit(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除菜单访问日志记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMenuVisit(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('menuVisit/menuVisit/export', {
        ...this.queryParams
      }, `menuVisit_menuVisit.xlsx`);
    },
    handleVisitTimeChange(v) {
      if (v) {
        this.queryParams.visitTimeStart = v[0] + " 00:00:00";
        this.queryParams.visitTimeEnd = v[1] + " 23:59:59";
      } else {
        this.queryParams.visitTimeStart = null;
        this.queryParams.visitTimeEnd = null;
        this.getList();
      }
    },
  }
};
</script>